//PITCH PROGRAM
//CLASS: HUMAN_PLAYER
//	-methods for human to bid, determine bid suit,
//	show cards, and play cards
//BY: ALEX TRACY
import java.util.Scanner;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Human_Player extends Player
{
	Scanner input = new Scanner(System.in);
	ML ml; //listener for mouse clicks 
	MML mml;//listener for mouse movement


	public Human_Player(int i)
	{
		super(i);
	}//End Constructor
	
	//GENERAL FUNCTIONS//
	
	//initialize panel with images of cards
	public void initPanel()
	{
	
		for(int i = 0; i<this.getHand().length; i++)
		{	
			
				this.setCardImage(i, 
						Card.getCardImg(getCard(i)));
		}	
	}//End initPanel
	
	//return true
	public boolean isHuman()
	{
		return true;
	}//End isHuman
	
	//BID FUNCTIONS//
	
	//determine bid of human by showing a joptionpane
	//with bids available until a valid bid is entered
	public void determineBid(int high)
	{
		if (high == 0)
			setBid(Integer.parseInt(JOptionPane.showInputDialog
					(new JPanel(), "Please enter a bid : 0 to pass or 2 - 4")));
		else
			setBid(Integer.parseInt(JOptionPane.showInputDialog
					(new JPanel(), "Please enter a bid : 0 to pass or " + high 
							+ "- 4")));
		boolean exit = false;
		while (exit == false) 
		{
			if (getBid() > high || getBid() == 0)
			{
				if (getBid() == 0 || getBid() == 2 || getBid() == 3 || getBid() == 4)
				exit = true;
			}
			else
			{	if (high == 0)
					setBid(Integer.parseInt(JOptionPane.showInputDialog
							(new JPanel(), "Please enter a bid : 0 to pass or 2 - 4")));
				else
					setBid(Integer.parseInt(JOptionPane.showInputDialog
							(new JPanel(), "Please enter a bid : 0 to pass or " + high 
									+ "- 4")));
			}
		}
	}//End determineBid
	
	//determine bid suit by showing a joptionpane with options
	//until user enters a valid option
	public void determineBidS()
	{
		String temp;
		boolean exit = false;
		while (exit == false)
		{
			temp = JOptionPane.showInputDialog(
					new JPanel(),"Please enter a suit: H , D, S, C");
			if ((temp.equals("H") || temp.equals("h")))
			{
				this.setBidS(0);
				exit = true;
			}
			else if (temp.equals("D") || temp.equals("d"))
			{
				this.setBidS(1);
				exit = true;
			}
			else if (temp.equals("C") || temp.equals("c"))
			{
				this.setBidS(2);
				exit = true;
			}
			else if (temp.equals("S") || temp.equals("s"))
			{
				this.setBidS(3);
				exit = true;
			}
		}
	}//End determineBidS
	
	//PLAY FUCNTIONS//
	
	//play card by adding a mouse motion lister and mouse lister
	//to panel, and returning card selected
	public int play(int[] p, int l, int t,int turn, int b, boolean jO, boolean tO
			,int hN)
	{
		ml = new ML();
		mml = new MML();
		pPanel.setPlayed(-1);
		pPanel.addMouseListener(ml);
		pPanel.addMouseMotionListener(mml);
		pPanel.repaint();
		return playCard(l,t,hN);
	}//end play
	
	//return card if a valid card is selected
	public int playCard(int led, int trump,int hN)
	{
		int tempCard = -1;
		boolean exit = false;
		while (!exit)
		{
			if (pPanel.getPlayed() != -1)
			{
				if (led == -1)
				{
					if ((hN == 0) && 
							(Card.getSuit(getCard
									(pPanel.getPlayed())) == trump))
					{
						int tempPos = this.pPanel.getPlayed();
						pPanel.removeMouseListener(ml);
						pPanel.removeMouseMotionListener(mml);
						tempCard = this.getCard(tempPos);
						pPanel.setPlayed(-1);
						this.setHand(tempPos,-1);
						pPanel.setSelected(-1);
						exit = true;
					}
					else if ((hN == 0) && 
							(Card.getSuit(getCard(pPanel.getPlayed()))
									!= trump))
						
					{
						JOptionPane.showMessageDialog(
								new JFrame(),"You Must Throw Trump on First Throw.");
						pPanel.setPlayed(-1);
					}
					else
					{
						int tempPos = this.pPanel.getPlayed();
						pPanel.removeMouseListener(ml);
						pPanel.removeMouseMotionListener(mml);
						tempCard = this.getCard(tempPos);
						pPanel.setPlayed(-1);
						this.setHand(tempPos,-1);
						pPanel.setSelected(-1);
						exit = true;
					}
				}
				else
				{
					if ( ( Card.getSuit(getCard(pPanel.getPlayed()))
							== led ) || 
							( Card.getSuit(getCard(pPanel.getPlayed()))
							== trump) ) 
					{	
						int tempPos = this.pPanel.getPlayed();
						pPanel.removeMouseListener(ml);
						pPanel.removeMouseMotionListener(mml);
						tempCard = this.getCard(tempPos);
						pPanel.setPlayed(-1);
						this.setHand(tempPos,-1);
						pPanel.setSelected(-1);
						exit = true;
					}	
					else
					{
						
						if (this.anyLeft(led) == true )
						{
							JOptionPane.showMessageDialog(
									new JFrame(),"You Must Follow Suit or Throw Trump");
							pPanel.setPlayed(-1);
						}
						else
						{
							tempCard = this.getCard(pPanel.getPlayed());
							int tempPos = this.pPanel.getPlayed();
							pPanel.removeMouseListener(ml);
							pPanel.removeMouseMotionListener(mml);
							pPanel.setPlayed(-1);
							this.setHand(tempPos,-1);
							pPanel.setSelected(-1);
							exit = true;
						}
					}
				}
			}
		}
		return tempCard;
	}//End playCard
	
	//return true if any cards are left in specified suit
	//false otherwise
	private boolean anyLeft(int l)
	{
		boolean anyLeft = false;
		for (int i = 0 ; i < this.getHand().length;i++)
		{
			if (this.getCard(i) == -1)
			{
				
			}
			else if (Card.getSuit(this.getCard(i)) == l)
			{
				anyLeft = true;
				break;
			}
		}
		return anyLeft;
	}//End anyLeft
	
	//MOUSE LISTENERS//
	
	//lister to see if mouse has moved into a card
	//if it has pop the card up
	private class MML implements MouseMotionListener
	{
		public void mouseMoved(MouseEvent e)
		{
			for (int i = 0 ; i < getHand().length ; i++)
			{
				if (e.getX() > (getHand().length-1) * pPanel.getSpacing())
					pPanel.setSelected(getHand().length-1);
				else if (e.getX() > i * pPanel.getSpacing()  && e.getX() < (i + 1) * pPanel.getSpacing())
					pPanel.setSelected(i);
				pPanel.repaint();
			}
		}//End MouseMoved;

		//no mouse dragged implementation
		public void mouseDragged(MouseEvent e){
		}//end mouseDragged

		
	}
	
	//mouse listener to determine if a card has been selected
	private class ML implements MouseListener
	{
		public void mouseClicked(MouseEvent e)
		{
			for (int i = 0 ; i < getHand().length ; i++)
			{
				if (e.getX() > (getHand().length-1) * pPanel.getSpacing())
					pPanel.setPlayed(getHand().length-1);
				else if (e.getX() > i * pPanel.getSpacing()  && 
						e.getX() < (i + 1) * pPanel.getSpacing())
					pPanel.setPlayed(i);
				pPanel.repaint();
			}
		}
		
		//no implementation
		public void mouseEntered(MouseEvent e){}
		public void mouseExited(MouseEvent arg0) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent arg0) {}
	
	}//end MouseListener
}//End Human_Player